package com.xexun.gpstracker.vo;

import android.content.SharedPreferences;
import android.util.Log;

import com.xexun.gpstracker.AppContext;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.common.MyConstants;
import com.xexun.gpstracker.util.Utils;
import org.json.JSONException;
import org.json.JSONObject;

import com.xexun.gpstracker.inter.HttpCallback;
import com.xexun.gpstracker.util.CommUtil;

/**
 * Created by HH
 * Date: 2015/7/31 0031
 * Time: 下午 4:28
 */
public class Cmd {
    public final static int INVALID = -1;
    public final static int PROCESSED = 0;
    public final static int SHOWCMD = 1;
    /**
     * 指令类型
     */
    private String type;
    /**
     * 指令内容，相当于后台的ti参数，很多指令不用填这个
     */
    private String cont;
    /**
     * 终端的IMEI
     */
    private String imei;

    /*
    收到指令的返回结果
     */
    private int result;

    /*
    收到指令的返回显示字串
     */
    public String showCont;

    /**
     * 自定义回调
     */
    private HttpCallback callback;

    /**
     * 隐藏响应消息显示
     */
    private boolean hideMessage = false;

    public Cmd() {
    }

    public Cmd(String type, String cont, String imei) {
        this.type = type;
        this.cont = cont;
        this.imei = imei;
    }

    public Cmd(String type, String cont, String imei, HttpCallback callback) {
        this.type = type;
        this.cont = cont;
        this.imei = imei;
        this.callback = callback;
    }

    public Cmd(String type, String cont, String imei, boolean hideMessage) {
        this.type = type;
        this.cont = cont;
        this.imei = imei;
        this.hideMessage = hideMessage;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCont() {
        return cont;
    }

    public void setCont(String cont) {
        this.cont = cont;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int value) {
        this.result = value;
    }

    public String getShowCont() {
        return showCont;
    }

    public void setShowCont(String cont) {
        this.showCont = cont;
    }

    public String getImei() {
        return imei;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public HttpCallback getCallback() {
        return callback;
    }

    public void setCallback(HttpCallback callback) {
        this.callback = callback;
    }

    public boolean isHideMessage() {
        return hideMessage;
    }

    public void setHideMessage(boolean hideMessage) {
        this.hideMessage = hideMessage;
    }

    public String toJson() {
        JSONObject json = new JSONObject();
        try {
            json.put("type", type);
            if (CommUtil.isNotBlank(cont)) {
                json.put("content", cont);
            }
        } catch (Exception e) {
            Log.e(Cmd.class.getName(), "转换JSON出错", e);
        }
        return json.toString();
    }

    public static Cmd parseCmd(JSONObject json) {
        String type = json.optString("type");
        String cont = json.optString("cont");
        String imei = json.optString("imei");

        Cmd cmd = execCmd(type, cont, imei);
        return cmd;
    }

    private static Cmd execCmd(String type, String cont, String imei) {
        SharedPreferences mSharedPreferences = Utils.getPreferences(AppContext.getContext());
        CmdType cmdType = CmdType.getType(type);

        Cmd cmd = new Cmd(type, cont, imei);
        switch(cmdType) {
            case TT:
                int intervalTime = Integer.parseInt(cont);
                Constants.loctionInterval = intervalTime;
                SharedPreferences.Editor editor = mSharedPreferences.edit();
                editor.putInt(MyConstants.LOCATION_INTERVAL_TIME, intervalTime);
                editor.apply();
                cmd.result = PROCESSED;
                break;

            case KQ:
                cmd.result = SHOWCMD;
                cmd.showCont = AppContext.getContext().getString(R.string.atdreq);
                break;

            default:
                break;
        }

        return cmd;
    }
}
